Skip to main content

Object reference not set to an instance of an object

报错信息

NullReferenceException: Object reference not set to an instance of an object TileManage.Start () (at Assets/Scriput/TileManage.cs:65)

原因

List<Goods> goodsList;

    //定义goods的数据类型

public struct Goods
{
public int goods_id;
public int grid_x;
public int grid_y;
}

Goods goods1, goods2, goods3;
List<Goods> goodsList;


private void Start()
{
goods1 = new Goods();
goods1.goods_id = 0;
goods1.grid_x = 2;
goods1.grid_y = 2;


goods2 = new Goods();
goods2.goods_id = 1;
goods2.grid_x = 2;
goods2.grid_y = 2;

goods3 = new Goods();
goods3.goods_id = 2;
goods3.grid_x = 1;
goods3.grid_y = 1;

goodsList.Add(goods1);
goodsList.Add(goods2);
goodsList.Add(goods3);

print(goodsList);
}

解决方法

List<Goods> goodsList;

改为

List<Goods> goodsList = new List<Goods>();